import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.UIManager;
import javax.swing.text.DefaultCaret;

public class VistaStreetView extends javax.swing.JFrame {

	private static final long serialVersionUID = 1L;

	private StreetView streetView;
	
    public VistaStreetView() {
    	streetView = new StreetView(this);
        initComponents();
    }

    private void initComponents() {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		
		jScrollPane1 = new javax.swing.JScrollPane();
        textArea = new javax.swing.JTextArea();
        calibrarBtn = new javax.swing.JButton();
        resetBtn = new javax.swing.JButton();
        puertoTxt = new javax.swing.JTextField();
        puertoLbl = new javax.swing.JLabel();
        iniciarBtn = new javax.swing.JButton();
        detenerBtn = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);
        setTitle("Demo StreetView");
        
        textArea.setColumns(20);
        textArea.setRows(5);
        jScrollPane1.setViewportView(textArea);

        calibrarBtn.setText("Calibrar");
        calibrarBtn.setEnabled(false);
        
        resetBtn.setText("Resetear Calibración");
        resetBtn.setEnabled(false);
        
        puertoLbl.setText("Puerto:");

        iniciarBtn.setText("Iniciar");

        detenerBtn.setText("Detener");
        detenerBtn.setEnabled(false);
        
        DefaultCaret caret = (DefaultCaret)textArea.getCaret();
        caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
        
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(42, 42, 42)
                        .addComponent(calibrarBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(36, 36, 36)
                        .addComponent(resetBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(25, 25, 25)
                        .addComponent(puertoLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(puertoTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(iniciarBtn)
                        .addGap(18, 18, 18)
                        .addComponent(detenerBtn))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 354, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(puertoTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(puertoLbl)
                    .addComponent(iniciarBtn)
                    .addComponent(detenerBtn))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(calibrarBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(resetBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(25, Short.MAX_VALUE))
        );
        
        
        iniciarBtn.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					streetView.listen(Integer.parseInt(puertoTxt.getText()));
					iniciarBtn.setEnabled(false);
					detenerBtn.setEnabled(true);
					calibrarBtn.setEnabled(true);
					resetBtn.setEnabled(true);
				} catch (Exception ex) {}
			}});
        
        detenerBtn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				streetView.stop();
				iniciarBtn.setEnabled(true);
				detenerBtn.setEnabled(false);
				calibrarBtn.setEnabled(false);
				resetBtn.setEnabled(false);
			}
		});
        
        calibrarBtn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				streetView.calibrate();
			}
		});
        
        resetBtn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				streetView.resetCalibration();
			}
		});
        
        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>
    
    public void update(final String s) {
    	java.awt.EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				textArea.append(s + "\n");
			}
		});
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new VistaStreetView().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JButton calibrarBtn;
    private javax.swing.JButton detenerBtn;
    private javax.swing.JButton iniciarBtn;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea textArea;
    private javax.swing.JLabel puertoLbl;
    private javax.swing.JTextField puertoTxt;
    private javax.swing.JButton resetBtn;
    // End of variables declaration

}
